package com.gxa.saas.controller;

import com.gxa.saas.pojo.Order;

import com.gxa.saas.pojo.OrderAdd;
import com.gxa.saas.pojo.User;
import com.gxa.saas.service.OrderService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import jdk.nashorn.internal.scripts.JS;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/sales")
@CrossOrigin
public class SalesController {
    @Autowired
    OrderService orderService;
    /**
     *
     * @return
     */
    @ApiOperation(value = "订单页添加接口",produces = "query")
    @ApiImplicitParams({


    })
    @PostMapping("/addOrder")
    public Map insertOrder(){
        Order order = new Order();
        Date date=new Date();
        order.setCreateTime(date);
        Integer insert = orderService.insert(order);

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("添加成功");
        jsonResult.set("id",order.getId());
        return jsonResult.getMap();
    }
    @ApiOperation(value = "分页查询",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query")
    })
    @PostMapping("/orderlist")
    public Map selectByPage(@RequestParam(value = "page",required = true)Integer page,
                            @RequestParam(value = "size",required = false,defaultValue = "10") Integer size){
        List<Order> orders = orderService.selectByPage(page, size);
        List<Order> orders1 = orderService.selectAll();

        int count=orders1.size();

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setCount("查询成功");
        jsonResult.set("count",count);
        jsonResult.setData(orders);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "删除操作",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "删除的id",paramType = "query"),

    })
    @PostMapping("/delete")
    public Map delete(Integer id , Map map){
        Integer delete = orderService.delete(id);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCount("200");
        jsonResult.setCount("修改成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "导出接口",produces = "query")
    @PostMapping("/export")
    public void export(@RequestParam(value = "from",required = false) String from,
                       @RequestParam(value = "to",required = false) String to,
                       HttpServletResponse response)throws Exception{
        //1.获取集合
        List<Order> orders=null;

        orders = orderService.selectAll();
        // 工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("订单列表");
        String fileName = "订单列表.xls";
        // 将文件名中的中文部分被URL编码后的内容进行解析, 最终还是显示为中文状态
        fileName = URLEncoder.encode(fileName,"utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "编号", "销售类型","金额合计","创建日期","订单类型","客户类型","客户名称","业务员","出库仓库","配送车辆"};
        // 创建一个行对象, 作为表头
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            // 在行中添加单元格
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            // 把字段名添加到表头行上
            cell.setCellValue(text);
        }

        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 添加数据行:表头信息占用第一行,所有要从1(第二行开始添加)
        int rowNum = 1;
        for (Order item : orders) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);
            dataRow.createCell(1).setCellValue(item.getSellType());
            dataRow.createCell(2).setCellValue(item.getMoney());
            dataRow.createCell(3).setCellValue(item.getCreateTime() );
            dataRow.createCell(4).setCellValue(item.getOrderType());
            dataRow.createCell(5).setCellValue(item.getClientType());
            dataRow.createCell(6).setCellValue(item.getClientName());
            dataRow.createCell(7).setCellValue(item.getSalesman());
            dataRow.createCell(8).setCellValue(item.getWareHouse());
            dataRow.createCell(9).setCellValue(item.getCar());
            //时间格式赋值

            rowNum++;
        }

        //返回给前端下载
        //指定文档类型
        response.setContentType("application/octet-stream");
        //指定响应头 Content-disposition附件
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        //刷新流
        response.flushBuffer();
        //输出流反给前端
        workbook.write(response.getOutputStream());


    }

    @ApiOperation(value = "排序查询",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sort",value = "需要传 降序/升序",paramType = "query"),
            @ApiImplicitParam(name = "page",value = "页数",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页记录数",paramType = "query")
    })
    @PostMapping("/selectBySortList")
    public Map selectBySort(@RequestParam(value = "sort",required = true)String sort,
                            @RequestParam(value = "page",required = true)Integer page,
                            @RequestParam(value = "size",required = false,defaultValue = "10") Integer size
                            ){

        List<Order> orders=new ArrayList<>();
        if ("降序".equals(sort)){
             orders = orderService.selectByDesc(page,size);
        }else{
            orders = orderService.selectByAsc(page,size);
        }

        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setCount("查询成功");
        jsonResult.setData(orders);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "下拉框内容 三个用一个接口 时间不能调用",produces = "query")
    @ApiImplicitParams({
    })
    @PostMapping("/selectList")
    public Map selectList(
    ){
        List<OrderAdd> orderAdds = orderService.selectList();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setCount("查询成功");
        jsonResult.setData(orderAdds);
        return jsonResult.getMap();

    }

    @ApiOperation(value = "下拉框联合查询",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "from",value = "起始时间",paramType = "query"),
            @ApiImplicitParam(name = "to",value = "结束时间",paramType = "query"),
            @ApiImplicitParam(name = "sellType",value = "销售类型",paramType = "query"),
            @ApiImplicitParam(name = "orderType",value = "订单类型",paramType = "query"),
            @ApiImplicitParam(name = "clientName",value = "客户类型",paramType = "query"),

    })
    @PostMapping("/selectByName")
    public Map selectByName(
            @RequestParam(value = "from",required = false)String from,
            @RequestParam(value = "to",required = false)String to,
            @RequestParam(value = "sellType",required = false)String sellType,
            @RequestParam(value = "orderType",required = false)String orderType,
            @RequestParam(value = "clientName",required = false)String clientName
    ){
        Map map=new HashMap();
        map.put("from",from);
        map.put("to",to);
        map.put("sellType",sellType);
        map.put("orderType",orderType);
        map.put("clientName",clientName);
        List<Order> orders = orderService.selectByName(map);
        System.out.println(orders);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setCount("查询成功");
        jsonResult.setData(orders);
        return jsonResult.getMap();
    }


}
